package com.pingan.haofang.searchcloud.index.dao;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.pingan.haofang.searchcloud.dashboard.dto.ProgressAggregateDto;
import com.pingan.haofang.searchcloud.index.domain.IndexBuildProgress;
import com.pingan.haofang.searchcloud.index.dto.IndexBuildProgressCountDTO;

/**
 * 索引执行过程DAO
 * 
 * @author LUYI374
 * @date May 16, 2017
 * @since 1.0.0
 */
@Repository
public interface IndexBuildProgressDao extends JpaRepository<IndexBuildProgress, Long>, JpaSpecificationExecutor<IndexBuildProgress>  {

    /**
     * 查找
     * 
     * @param projectCode
     * @param indexName
     * @param indexBuildType
     * @param status
     * @param startTime
     * @param page
     * @return
     */
    @Query(value = "select e from IndexBuildProgress e where e.projectCode = :projectCode and e.indexName = :indexName "
            + "and e.startTime between :beginDate and :endDate "
            + "and (:indexBuildType < 0 or e.indexBuildType = :indexBuildType) and (:status < 0 or e.status = :status) "
            + "order by e.indexBuildType desc, e.id desc")
    Page<IndexBuildProgress> query(@Param("projectCode") String projectCode, @Param("indexName") String indexName,
            @Param("indexBuildType") int indexBuildType, @Param("status") int status,
            @Param("beginDate") Date beginDate, @Param("endDate") Date endDate, Pageable page);

    /**
     * 查询
     * 
     * @param projectCode
     * @param indexName
     * @param indexBuildType
     * @param status
     * @return
     */
    @Query("select e from IndexBuildProgress e where e.projectCode = :projectCode and e.indexName = :indexName "
            + "and e.indexBuildType = :indexBuildType and e.status = :status")
    List<IndexBuildProgress> query(@Param("projectCode") String projectCode, @Param("indexName") String indexName,
            @Param("indexBuildType") int indexBuildType, @Param("status") int status);

    /**
     * 得到唯一的progress信息
     * 
     * @param projectCode
     * @param indexName
     * @param dataSourceName
     * @param indexBuildType
     * @return
     */
    @Query("select e from IndexBuildProgress e where e.projectCode = :projectCode and e.indexName = :indexName "
            + "and e.datasourceName = :dataSourceName and e.indexBuildType = :indexBuildType")
    IndexBuildProgress get(@Param("projectCode") String projectCode, @Param("indexName") String indexName,
            @Param("dataSourceName") String dataSourceName, @Param("indexBuildType") int indexBuildType);



    @Query("select new com.pingan.haofang.searchcloud.dashboard.dto.ProgressAggregateDto(a.indexName,a.runType,a.status,a.datasourceName,count(a)" +
            ",max(a.importSuccessCount),sum(a.executeCostTime),sum(a.importSuccessCount)) from IndexBuildProgress a " +
            "where a.indexBuildType = :indexBuildType and a.startTime between :startDate and :endDate " +
            "group by a.indexName,a.runType,a.status,a.datasourceName")
    List<ProgressAggregateDto> findProgressAggegateDto(@Param("indexBuildType") Integer indexBuildType,
                                                       @Param("startDate") Date startDate, @Param("endDate") Date endDate);

    List<IndexBuildProgress>  findByProjectCodeAndIndexNameAndIndexBuildTypeAndStatusOrderByStartTimeDesc(String group, String name, int type, int status);
    
    /**
     * 时间段内状态为status的记录
     */
    @Query(value = "select e from IndexBuildProgress e where e.updateTime >= :startTime and e.updateTime <= :endTime and e.status in (:status)")
    List<IndexBuildProgress> queryByStatusAndUpdateTime(@Param("startTime") Date startTime, @Param("endTime") Date endTime, @Param("status") List<Integer> status);


    @Query(value = "select new com.pingan.haofang.searchcloud.index.dto.IndexBuildProgressCountDTO(e.projectCode,e.indexName,e.status,count(e),SUM(e.importSuccessCount),SUM(e.executeCostTime))" +
            " from IndexBuildProgress e where e.updateTime >= :startTime and e.updateTime <= :endTime and e.status in (:status) group by e.projectCode,e.indexName,e.status")
    List<IndexBuildProgressCountDTO> queryIndexBuildProgressCountList(@Param("startTime") Date startTime, @Param("endTime") Date endTime, @Param("status") List<Integer> status);
}
